#include "xchartview.h"
#include <QDebug>

XChartView::XChartView(XCustomChart* pChart, QWidget* parent)
    :QChartView(parent)
    ,m_chart(pChart)
{
    isPressed=false;
    setDragMode(QChartView::NoDrag);    //忽略拖动
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //垂直无滚轴
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);   //水平无滚轴
    this->setMouseTracking(true);   //监听鼠标
}

XChartView::~XChartView()
{

}

void XChartView::mousePressEvent(QMouseEvent *event)
{
    //使用鼠标左键
    if(event->button()==Qt::LeftButton)
    {
        isPressed=true; //左键按下
        m_ptLast=event->globalPos();  //保存该坐标
    }
    QChartView::mousePressEvent(event);
}

void XChartView::mouseMoveEvent(QMouseEvent *event)
{
    //这里不可以用event->button()来判断鼠标坐标按下
    //只能打个标志
    //Note that the returned value is always Qt::NoButton for mouse move events.
    //官方解释
    if(isPressed)
    {
        if(chart()!=NULL)
        {
            m_chart->modifyCurve(event->pos());
            m_ptLast=event->globalPos();
        }
    }
    QChartView::mouseMoveEvent(event);
}

void XChartView::mouseReleaseEvent(QMouseEvent *event)
{
    isPressed=false;    //左键放开
    if(event->globalPos()!=m_ptLast)
    {
        m_chart->modifyCurve(event->pos()); //鼠标抬起时，更新曲线坐标
        m_chart->setModifyFlag(false);
    }
    QChartView::mouseReleaseEvent(event);
}
